package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    @Select("update orders set status = #{orderPaidStatus}, pay_status = #{orderStatus}, checkout_time = #{checkOutTime} where number = #{orderNumber}")
    void updateStatus(Integer orderPaidStatus, Integer orderStatus, LocalDateTime checkOutTime, String orderNumber);

    /**
     * 跳过微信支付直接修改数据库订单信息
     * @param orders:
     * @return void
     */
    @Update("update orders set pay_status = #{payStatus}, status = #{status}, checkout_time = #{checkoutTime} " +
            "where number = #{number}")
    void updateByOrderNumber(Orders orders);

    @Select("select count(id) from orders where status=#{status}")
    Integer getStatusNumber(Integer status);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
//    @Select("select * from orders where id = #{id}")
//    Orders getByID(Long id);

//    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);


    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询订单
     * @param id
     */
    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    @Select("select * from orders where status=#{status} and order_time < #{time}")
    List<Orders> getByStatusAndTime(Integer status, LocalDateTime time);

    @Select("select sum(amount) from orders where order_time>=#{beginTime} and order_time < #{endTime} and status=#{status}")
    Double getTurnoverByDate(LocalDateTime beginTime, LocalDateTime endTime, Integer status);

    Integer getOrderByDate(Map map);

    @Select(("select od.name, sum(od.number) number from orders o , order_detail od" +
            " where o.id = od.order_id and o.status=5\n and order_time >= #{beginTime} and order_time<#{endTime}\n" +
            "group by od.name\n" +
            "order by number desc\n" +
            "limit 10;"))
    List<GoodsSalesDTO> getTop10(LocalDateTime beginTime, LocalDateTime endTime);
}
